package com.android.pocketguru.Fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.pocketguru.Constants;
import com.android.pocketguru.Phrases;
import com.android.pocketguru.R;

public class ShareFragment extends Fragment{

	private int phraseId = -1;
	private String NewLine = System.getProperty("line.separator");
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.share, container, false);
    }
    
    @Override
    public void onStart() {
        super.onStart();
        
        Bundle args= getArguments();
        if (args !=null)
        	phraseId = args.getInt(Constants.PHRASE_ID_KEY);
        
        if (phraseId==-1)
        	phraseId = Phrases.GetRandomPhraseId(getActivity().getBaseContext());
        
        TextView txt = (TextView) getActivity().findViewById(R.id.txtPhrase);
        txt.setText(Phrases.GetPhraseById(getActivity().getBaseContext(), phraseId));
        
        // Test of built-in SEND content functionality
        final Button btnBuiltInShare = (Button) getActivity().findViewById(R.id.btnBuiltInShare);
        btnBuiltInShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	Intent shareIntent = new Intent(Intent.ACTION_SEND);
            	shareIntent.setType("text/plain");
            	
            	shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Wisdom " );
            	shareIntent.putExtra(Intent.EXTRA_TEXT, Phrases.GetPhraseById(getActivity().getBaseContext(), phraseId) 
            						+ NewLine
            						+ NewLine 
            						+ "From Some Android APP.");
        
            	startActivity(Intent.createChooser(shareIntent, "Share..."));
            }
        });
    }
}
